# -*- coding:utf-8 -*-

import csv
from pymongo import MongoClient
import re
from html import unescape

def html_to_plain_text(html):
    text = re.sub('<head.*?>.*?</head>', '', html, flags=re.M | re.S | re.I)
    text = re.sub('<a\s.*?>', ' HYPERLINK ', text, flags=re.M | re.S | re.I)
    # text = re.sub('<.*?>', '', text, flags=re.M | re.S)
    # 过滤标签，保留标签p
    text = re.sub('(?!<(p).*?>|<(/p).*?>)<.*?>', '', text, flags=re.M | re.S)
    text = re.sub('(?!<(P).*?>|<(/P).*?>)<.*?>', '', text, flags=re.M | re.S)
    # text = re.sub(r'(\s*\n)+', '\n', text, flags=re.M | re.S)

    # =======
    return unescape(text)


client = MongoClient('127.0.0.1', 27017)
db = client["Test"]
table = db["初级经济法基础2"]
# datas = table.find({"$and":[{"type": ""}, {"title": ""}]})
datas = table.find()
line = 0
previous = ""
for d in datas:
    dict_data = dict()
    dict_data['科目'] = d.get('科目')
    dict_data['类型'] = d.get('类型')
    dict_data['章'] = d.get('章')
    dict_data['节'] = d.get('节')
    dict_data['题型'] = d.get('题型')
    dict_data['问题'] = html_to_plain_text(d.get('标题')).replace("\n", "")
    dict_data['子问题'] = d.get('子问题')
    if d.get('选项_1') is not None:
        dict_data['选项A'] = d.get('选项_1').replace(".", "").replace("(A)", "").replace("(B)", "").replace("(C)", "").replace("(D)", "")
    if d.get('选项_2') is not None:
        dict_data['选项B'] = d.get('选项_2').replace(".", "").replace("(A)", "").replace("(B)", "").replace("(C)", "").replace("(D)", "")
    if d.get('选项_3') is not None:
        dict_data['选项C'] = d.get('选项_3').replace(".", "").replace("(A)", "").replace("(B)", "").replace("(C)", "").replace("(D)", "")
    if d.get('选项_4') is not None:
        dict_data['选项D'] = d.get('选项_4').replace(".", "").replace("(A)", "").replace("(B)", "").replace("(C)", "").replace("(D)", "")
    if d.get('解析') is not None:
        dict_data['解析'] = html_to_plain_text(d.get('解析')).replace("\n", "")
    dict_data['答案'] = d.get('答案')

    print(str(dict_data))
    if dict_data['节'] != "":
        path = "./csv/" + dict_data['科目'] + "_" + dict_data['章'] + "_" + dict_data['节'] + ".csv"
    else:
        path = "./csv/" + dict_data['科目'] + "_" + dict_data['章'] + ".csv"

    if previous != path:
        previous = path
        with open(path, "a+", encoding="utf-8-sig", newline="") as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow(["科目", "类型", "章", "节", "题型",
                                 "问题", "子问题", "解析", "答案", "选项A", "选项B", "选项C", "选项D"])

    with open(path, "a", encoding="utf-8-sig", newline="") as csvfile:
        fileheader = ["科目", "类型", "章", "节", "题型",
                      "问题", "子问题", "解析", "答案", "选项A", "选项B", "选项C", "选项D"]
        writer = csv.DictWriter(csvfile, fieldnames=fileheader)
        writer.writerow(dict_data)
    line += 1
    print(line)